package com.lianxi.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ResponseUtil {

    //成功并且把数据转换为Json格式
    public static void successDataToJson(HttpServletResponse response, Object data) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        CommonResult<Object> success = CommonResult.success(data);
        String json = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).writeValueAsString(success);
        response.getWriter().write(json);
    }

    //失败并且把数据转换为Json格式
    public static void errorDataToJson(HttpServletResponse response, String mesage) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        CommonResult<Object> success = CommonResult.error(mesage);
        String json = new ObjectMapper().writeValueAsString(success);
        response.getWriter().write(json);
    }

}
